package com.example.ordersystem.controller;

import com.example.ordersystem.entity.Courier;
import com.example.ordersystem.entity.vo.ResultMessage;
import com.example.ordersystem.service.CourierService;
import com.example.ordersystem.util.EmailUtil;
import com.example.ordersystem.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 配送员控制类
 */
@RestController
@RequestMapping("order/courier")
@CrossOrigin
public class CourierController {
    @Autowired
    CourierService courierService;

    @Autowired
    ResultUtil resultUtil;

    /**
     * 发送邮箱验证码
     */
    @GetMapping("{mailbox}")
    public ResultMessage sendCode(@PathVariable String mailbox)
    {
        String code= EmailUtil.creatCode(6);
        EmailUtil.sendEmail(code,mailbox);
        return resultUtil.data(code);
    }


    /**
     * 配送员注册
     */
    @PostMapping("register")
    public ResultMessage register(@RequestBody Courier courier)
    {
        System.out.println(courier);
           courierService.register(courier);
           return resultUtil.success("注册成功！");
    }

    /**
     * 配送员登录
     */
    @PostMapping("login")
    public ResultMessage login(@RequestBody HashMap<String,String>map)
    {
        Courier login = courierService.login(map.get("mailbox"), map.get("pwd"));
        if(login!=null)
         {
             return resultUtil.data(login);
         }
         return resultUtil.error("邮箱或密码错误");
    }

    /**
     * 根据id获取配送员个人信息
     */
    @GetMapping("getInfo/{id}")
    public ResultMessage getInfoById(@PathVariable String id)
    {
        return resultUtil.data(courierService.getInfoById(id));
    }

    /**
     * 修改配送员个人信息
     */
    @PostMapping("changeInfo")
    public ResultMessage changeInfo(@RequestBody Courier courier)
    {
        courierService.changeInfo(courier);
        return resultUtil.success("修改成功！");
    }

    /**
     * 修改配送员头像
     */
    @PostMapping("changeAvatar")
    public ResultMessage changeAvatar(@RequestBody Map<String,String> map)
    {
        courierService.changeAvatar(map.get("id"),map.get("avatar"));
        return resultUtil.success("修改成功");
    }
}
